var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store

// pages/mainWrite/location/location.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.stomp(1);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  stomp(stationId){
    if(!$.isNull(this.data.key)){//切换连接前关闭链接
      wx.closeSocket();
      clearInterval(this.data.setInterval_id);
    }
    var that=this;
    var context=this.data.context;
    var drawList=that.data.drawList;
    const yxs = [205, 207, 210, 211, 212, 214, 215, 216, 233];
    
    
    function fixZeroStart(str, n) {
      return (Array(n).join(0) + str).slice(-n);
    }
    
    const keyStation = fixZeroStart(parseInt(stationId / 65535).toString(), 4) + fixZeroStart((stationId % 65535).toString(16), 4);
    
    var socketOpen = false
    var socketMsgQueue = new Array()
    function sendSocketMessage(msg) {
        // console.log('send msg:')
        // console.log(msg);
        if (socketOpen) {
            wx.sendSocketMessage({
                data: msg
            })
        } else {
            socketMsgQueue.push(msg)
        }
    }
  
    /////////////////////////////////////////////////////
    var ws = {
        send: sendSocketMessage,
        onopen: null,
        onmessage: null
    }
    wx.connectSocket({
        // url: 'ws://129.211.164.79:15674/ws'
        // url:"wss://dg1.ruidcn.cn/wss",
        url:api.url.replace("https://","wss://")+"/wss"
    })
    wx.onSocketOpen(function (res) {
        console.log('WebSocket连接已打开！')
  
        socketOpen = true
        for (var i = 0; i < socketMsgQueue.length; i++) {
            sendSocketMessage(socketMsgQueue[i])
        }
        socketMsgQueue = []
  
        ws.onopen && ws.onopen()
    })
  
    wx.onSocketMessage(function (res) {
        // console.log('收到onmessage事件:', res)
        ws.onmessage && ws.onmessage(res)
    })
  
  
  
  
    var Stomp = require('../../../utils/stomp.js').Stomp;
    Stomp.setInterval = function () { }
    Stomp.clearInterval = function () { }
    var client = Stomp.over(ws);
  
    var destination1 = '/exchange/ruidcn/ruidcn.location.*' ;
  
  
    // client.connect('alarm', 'alarm', function (sessionId) {
      client.connect('admin', 'RDRJ321rdrj123RUId', function (sessionId) {
        
  
  
        // console.log('sessionId', sessionId)
  
        client.subscribe(destination1, function (body, headers) {
          var dist;
          try {
            // console.log(body.body)
            dist = JSON.parse( body.body );
            // dist = eval(body.body)
            console.log('From MQ 1:', dist);
            console.log('From MQ 1:', dist.longitude);
            } catch(e) {
              console.log('客户端发送心跳包MQ1:', body);
                return ;
            }

        }, {
          id: keyStation + ".ai"
        });
  

  
        var setInterval_id=setInterval(function(){//心跳包机制,防止断开连接
          // client.send(destination1, { priority: 9 }, 'hello !');
        },15000)
        that.setData({
          setInterval_id:setInterval_id
        })
  
  
        // if (that.data.key) {
        // 	client.unsubscribe(key + ".ai");
        // 	client.unsubscribe(key + ".di");
        // }
        that.setData({
          key:keyStation
        })
    })
  },
})